from django.shortcuts import render
import datetime
import logging
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import mixins, viewsets
from rest_framework.pagination import PageNumberPagination
from article.models import Article, Category, Race
from article.serializers import CategorySerializer, ArticlesSerializer, RacesSerializer,ArticleListSerializer, RaceListSerializer
from article.models import Category
from django.views.decorators.csrf import csrf_exempt
from pymysql import MySQLError
from article.forms import ArticleForm
from article.models import Article, Category
from users.models import UserProfile
from users.user_utils import check_token


def delete_article(request, post_id):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'GET':
        token = request.GET.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                try:
                    article = Article.objects.filter(id=post_id).first()
                    article.is_delete = True
                    article.delete_time = datetime.datetime.now()
                    article.save()
                    data = 1
                except MySQLError:
                    code = 30000
                    msg = "服务器错误"
            else:
                code = 10000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 10000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })


@csrf_exempt
def pub_article(request):
    code = 0
    msg = 'ok'
    data = 0
    if request.method == 'POST':
        token = request.POST.get('token', None)
        user_id = check_token(token)
        if user_id:
            user = UserProfile.objects.filter(id=user_id).first()
            if user:
                title = request.POST.get('title', None)
                content = request.POST.get('content', None)
                image = request.FILES.get('images', None)
                category_id = request.POST.get('category', None)
                category = Category.objects.filter(id=category_id).first()
                if category:
                    post_form = ArticleForm({"title": title, "content": content})
                    if post_form.is_valid():
                        try:
                            article = Article()
                            article.author = user
                            article.title = title
                            article.category = category
                            article.content = content
                            article.image = image
                            article.save()
                            data = 1
                        except MySQLError as e:
                            logging.error(e)
                            code = 30000
                            msg = "服务器出了点小差错"
                    else:
                        code = 10000
                        msg = post_form.errors.as_json()
                        print(msg)
                else:
                    code = 10000
                    msg = "不存在该分类"
            else:
                code = 10000
                msg = "不存在该用户"
        else:
            code = 10000
            msg = "无效的令牌"
    else:
        code = 10000
        msg = "请求方法错误"
    return JsonResponse({
        'code': code,
        'msg': msg,
        'data': data
    })



#分页器
class PaginationNum(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    page_query_param = "page"
    max_page_size = 20


#获取分类列表
class CategoryViewset(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer


#获取文章分类列表
class ArticleListViewset(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    queryset = Category.objects.all()
    serializer_class = ArticleListSerializer
    pagination_class = PaginationNum


#获取文章详情
class ArticlesViewset(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticlesSerializer
    pagination_class = PaginationNum


#获取赛事详情
class RacesViewset(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    queryset = Race.objects.all()
    serializer_class = RacesSerializer
    pagination_class = PaginationNum


#获取赛事列表列表
class RacesListViewset(mixins.ListModelMixin,mixins.RetrieveModelMixin,viewsets.GenericViewSet):
    queryset = Category.objects.all()
    serializer_class = RaceListSerializer
    pagination_class = PaginationNum


#修改文章POST/update_article
